package com.bw.producer.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.bw.common.core.domain.Result;
import com.bw.common.core.utils.DateUtils;
import com.bw.elastic.common.domin.Attrs;
import com.bw.elastic.common.domin.SkuEsModelList;
import com.bw.producer.domain.PmsBrand;
import com.bw.producer.domain.PmsCategory;
import com.bw.producer.domain.PmsSkuInfo;
import com.bw.producer.mapper.PmsBrandMapper;
import com.bw.producer.mapper.PmsCategoryMapper;
import com.bw.producer.mapper.PmsSkuInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bw.producer.mapper.PmsSpuInfoMapper;
import com.bw.producer.domain.PmsSpuInfo;
import com.bw.producer.service.IPmsSpuInfoService;
import com.bw.elastic.feign.ProducerFeign;

/**
 * spuService业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-19
 */
@Service
public class PmsSpuInfoServiceImpl implements IPmsSpuInfoService 
{
    @Autowired
    private PmsSpuInfoMapper pmsSpuInfoMapper;
    @Autowired
    private ProducerFeign producerFeign;
    @Autowired
    private PmsSkuInfoMapper pmsSkuInfoMapper;
    @Autowired
    private PmsBrandMapper pmsBrandMapper;
    @Autowired
    private PmsCategoryMapper pmsCategoryMapper;



    /**
     * 查询spu
     * 
     * @param id spu主键
     * @return spu
     */
    @Override
    public PmsSpuInfo selectPmsSpuInfoById(Long id)
    {
        return pmsSpuInfoMapper.selectPmsSpuInfoById(id);
    }

    /**
     * 查询spu列表
     * 
     * @param pmsSpuInfo spu
     * @return spu
     */
    @Override
    public List<PmsSpuInfo> selectPmsSpuInfoList(PmsSpuInfo pmsSpuInfo)
    {
        return pmsSpuInfoMapper.selectPmsSpuInfoList(pmsSpuInfo);
    }

    /**
     * 新增spu
     * 
     * @param pmsSpuInfo spu
     * @return 结果
     */
    @Override
    public int insertPmsSpuInfo(PmsSpuInfo pmsSpuInfo)
    {
        pmsSpuInfo.setCreateTime(DateUtils.getNowDate());
        return pmsSpuInfoMapper.insertPmsSpuInfo(pmsSpuInfo);
    }

    /**
     * 修改spu
     * 
     * @param pmsSpuInfo spu
     * @return 结果
     */
    @Override
    public int updatePmsSpuInfo(PmsSpuInfo pmsSpuInfo)
    {
        pmsSpuInfo.setUpdateTime(DateUtils.getNowDate());
        return pmsSpuInfoMapper.updatePmsSpuInfo(pmsSpuInfo);
    }

    /**
     * 批量删除spu
     * 
     * @param ids 需要删除的spu主键
     * @return 结果
     */
    @Override
    public int deletePmsSpuInfoByIds(Long[] ids)
    {
        return pmsSpuInfoMapper.deletePmsSpuInfoByIds(ids);
    }

    /**
     * 删除spu信息
     * 
     * @param id spu主键
     * @return 结果
     */
    @Override
    public int deletePmsSpuInfoById(Long id)
    {
        return pmsSpuInfoMapper.deletePmsSpuInfoById(id);
    }

    /**
     * 上架
     * @param spuId
     * @return
     */
    @Override
    public void Upload(Integer spuId) {
        //修改为上架
        pmsSpuInfoMapper.update(spuId);

        //sku列表
        List<PmsSkuInfo> skuList = getSkuInfoListBySpuId(spuId);

        //转换
        List<SkuEsModelList> list = transferSkuEsModelList(skuList,spuId);

        //添加到es中
        Result result = producerFeign.addEs(list);

        if (result.getCode()==200){

            PmsSpuInfo pmsSpuInfo = this.pmsSpuInfoMapper.selectPmsSpuInfoById(Long.valueOf(spuId));
            pmsSpuInfo.setPublishStatus(1L);
            pmsSpuInfoMapper.updatePmsSpuInfo(pmsSpuInfo);

        }
    }

    private List<PmsSkuInfo> getSkuInfoListBySpuId(Integer spuId) {
        PmsSkuInfo pmsSkuInfo = new PmsSkuInfo();
        pmsSkuInfo.setSpuId(Long.valueOf(spuId));
        List<PmsSkuInfo> pmsSkuInfos = pmsSkuInfoMapper.selectPmsSkuInfoList(pmsSkuInfo);

        return pmsSkuInfos;
    }

    private List<SkuEsModelList> transferSkuEsModelList(List<PmsSkuInfo> skuList, Integer spuId) {

        List<Attrs> attrs = pmsSpuInfoMapper.selectAttr(spuId);

        List<Attrs> attrsList = attrs.stream().map(attrvalue -> {
            Attrs attr = new Attrs();
            attr.setAttrId(attrvalue.getAttrId());
            attr.setAttrName(attrvalue.getAttrName());
            attr.setAttrValue(attrvalue.getAttrValue());

            return attr;


        }).collect(Collectors.toList());

        List<SkuEsModelList>  skuEsModelList = skuList.stream().map(pmsSkuInfo -> {
            SkuEsModelList skuEsModel = new SkuEsModelList();
            skuEsModel.setSkuId(pmsSkuInfo.getSkuId());
            skuEsModel.setSpuId(pmsSkuInfo.getSpuId());
            skuEsModel.setSkuTitle(pmsSkuInfo.getSkuTitle());
            skuEsModel.setSkuPrice(pmsSkuInfo.getPrice());
            skuEsModel.setSkuImg(pmsSkuInfo.getSkuDefaultImg());
            skuEsModel.setSaleCount(pmsSkuInfo.getSaleCount());
            skuEsModel.setHasStock(true);
            skuEsModel.setHotScore(100L);


            PmsBrand brand = pmsBrandMapper.selectPmsBrandByBrandId(pmsSkuInfo.getBrandId());
            skuEsModel.setBrandId(brand.getBrandId());
            skuEsModel.setBrandName(brand.getName());
            skuEsModel.setBrandImage(brand.getLogo());


            PmsCategory category = pmsCategoryMapper.selectPmsCategoryByCatId(pmsSkuInfo.getCatalogId());
            skuEsModel.setCatalogId(category.getCatId());
            skuEsModel.setCatalogName(category.getName());

            skuEsModel.setAttrs(attrsList);

            return skuEsModel;
        }).collect(Collectors.toList());

        return skuEsModelList;
    }
}
